﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using wh40klbModel;
using System.Threading.Tasks;

namespace wathammer40kListBuilder.ViewModel
{
    class AdminArmyViewModel : ViewModel
    {
        private bool waiting;

        public bool Waiting
        {
            get { return waiting; }
            set
            {
                waiting = value;
                onPropertyChanged("Waiting");
            }
        }


        private Army[] armies;
        public Army[] Armies
        {
            get { return armies; }
            set
            {
                armies = value;
                onPropertyChanged("Armies");
                Waiting = false;
            }
        }

        private Army selectedArmy;
        public Army SelectedArmy
        {
            get { return selectedArmy; }
            set
            {
                selectedArmy = value;

                majSelectedArmy();
                //onPropertyChanged("SelectedArmy");
            }

        }

        private List<Infantry> infantries;
        public List<Infantry> Infantries
        {
            get { return infantries; }
            set
            {
                infantries = value;
                onPropertyChanged("Infantries");
            }
        }
        private Vehicule[] vehicules;
        public Vehicule[] Vehicules
        {
            get { return vehicules; }
            set
            {
                vehicules = value;
                onPropertyChanged("Vehicules");
            }
        }

        public void majSelectedArmy()
        {
            using (WarhammerContext context = new WarhammerContext())
            {
                Infantries = context.Infantries.Where(i => i.Army.Id == selectedArmy.Id).ToList();
                Vehicules = context.Vehicules.Where(i => i.Army.Id == selectedArmy.Id).ToArray();
            }
        }

        public AdminArmyViewModel()
        {
            Waiting = true;
            Infantries = new List<Infantry>() { new Infantry { Name = "tolaria" } };
            Task t = new Task(() =>
            {
                using (WarhammerContext context = new WarhammerContext())
                {
                    Armies = context.Armies.OrderBy(a => a.Name).ToArray();
                }

            });
            t.Start();

        }



        internal void DeleteArmy()
        {

            Task t = new Task(() =>
            {
                using (WarhammerContext context = new WarhammerContext())
                {
                    foreach (var item in context.ArmySquadTypesUsed.Where(astu => astu.ArmyId == selectedArmy.Id))
                    {
                        context.Entry(item).State = System.Data.EntityState.Deleted;
                    }
                    foreach (var item in context.Infantries.Where(i => i.Army.Id == selectedArmy.Id))
                    {
                        context.Entry(item).State = System.Data.EntityState.Deleted;
                    }
                    foreach (var item in context.Vehicules.Where(i => i.Army.Id == selectedArmy.Id))
                    {
                        context.Entry(item).State = System.Data.EntityState.Deleted;
                    }
                    foreach (var item in context.Squads.Where(s => s.Army.Id == selectedArmy.Id))
                    {
                        context.Entry(item).State = System.Data.EntityState.Deleted;
                    }
                    foreach (var item in context.Wargears.Where(w => w.Army.Id == selectedArmy.Id))
                    {
                        context.Entry(item).State = System.Data.EntityState.Deleted;
                    }


                    context.Entry(selectedArmy).State = System.Data.EntityState.Deleted;
                    context.SaveChanges();
                }
            });



            t.Start();

        }
    }
}
